{% extends base.html %}
{% block body %}
    <div class="container-fluid">
        <!-- <div class="row">
            <div class="col-xs-2 p0">
                <h2>Console</h2>
            </div>
            <div class="col-xs-10 p0">
                <h2>Result</h2>
            </div>
        </div> -->

        <div class="row">

            <div class="col-xs-2 p0 bc-gray">
                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <ul class="nav" id="side-menu">
                            <li>
                                <a href="/">
                                    <i class="fa fa-bug fa-fw"></i> Home
                                </a>
                            </li>

                            <li>
                                <a href="/tasks" class="active">
                                    <i class="fa fa-list-ul fa-fw"></i> Result
                                </a>
                            </li>

                            <li>
                                <a href="/config">
                                    <i class="fa fa-cog fa-fw"></i> Config
                                </a>
                            </li>
                                  <li>
                                <a href="/exit" >
                                    <i class="fa fa-exit fa-fw"></i>Exit</a>
                            </li>

                            <!-- <li>
                                <a href="javascript:;">
                                    <i class="fa fa-bar-chart-o fa-fw"></i> None
                                    <span class="fa arrow"></span>
                                </a>
                                <ul class="nav nav-second-level">
                                    <li><a href="{:U('Baidu/index')}">百度音乐</a></li>
                                    <li><a href="{:U('Qq/index')}">QQ音乐</a></li>
                                    <li><a href="{:U('Netease/index')}">网易云音乐</a></li>
                                </ul>
                            </li> -->
                        </ul>
                    </div>
                </div>
            </div>

            <div class="col-xs-10">
                <div class="row">
                    <div class="col-lg-12">
                        <h3 class="page-header">Result</h3>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>

                <div class="row">
                    <div class="col-xs-12">
                        <div class="panel panel-default">
                            <!-- <div class="panel-heading">
                                <button class="btn btn-sm btn-primary" onclick="addDetail()">添加专辑</button>
                            </div> -->

                            <table class="table table-hover table-bordered">
                                <thead>
                                    <tr>
                                        <th class="text-center w50"><input type="checkbox" id="all-checkbox"></th>
                                        <th>
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="TaskID - 任务ID">
                                                ID
                                            </span>
                                        </th>
                                        <th style="min-width:100px;">
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="URL - 存在SQL注入的地址">
                                                URL
                                            </span>
                                        </th>
                                        <th class="hidden-xs hidden-sm text-center">
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="Method - 请求方式">
                                                Method
                                            </span>
                                        </th>
                                        <th class="hidden-xs hidden-sm">
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="PostData - 数据">
                                                PostData
                                            </span>
                                        </th>
                                        <th class="">
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="Payload - 攻击载荷">
                                                Payload
                                            </span>
                                        </th>
                                        <th class="">
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="Parameter - 尝试值">
                                                Parameter
                                            </span>
                                        </th>
                                        <th class="">
                                            <span data-toggle="tooltip" 
                                                  data-placement="top" 
                                                  title="Operating - 相关操作">
                                                Operating
                                            </span>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody id="tasklist">
                                    <!-- <tr><td colspan="7" class="text-center">Not Found List</td></tr> -->
                                    {% for task in tasks %}
                                    <tr>
                                        <td class="text-center"><input type="checkbox" value="{{ task['taskid'] }}"></td>
                                        <td>{{ task['taskid'] }}</td>
                                        <td>
                                            <a href="{{ task['url'] }}"
                                                data-toggle="tooltip" 
                                                data-placement="bottom" 
                                                title="{{ task['url'] }}"
                                                target="_black">
                                                {{ task['url'] }}
                                            </a>
                                        </td>
                                        <td class="hidden-xs text-center text-info">{{ task['method'] }}</td>
                                        <td class="hidden-xs hidden-sm">
                                            <div class="text-space wmax100"
                                                data-toggle="tooltip" 
                                                data-placement="bottom" 
                                                title="{{ task['postData'] }}">
                                                {{ task['postData'] }}
                                            </div>
                                        </td>
                                        <td class="hidden-xs">{{ task['payload'] }}</td>
                                        <td class="hidden-xs">{{ task['parameter'] }}</td>
                                        <td class="">
                                            <a href="javascript:;" 
                                                class="btn-xs btn-link" 
                                                onclick="viewModal( $(this) );">
                                                View
                                            </a>
                                            <a href="javascript:;" class="btn-xs btn-link" onclick="removeItem($(this))">Delete</a>
                                        </td>
                                    </tr>
                                    {% end %}
                                </tbody>
                            </table>
                            
                            <div class="panel-footer">
                                <div class="row">
                                    <div class="col-xs-4">
                                        <button type="button" 
                                            class="btn btn-xs btn-default" 
                                            id="btn-removeAll" 
                                            >
                                            批量删除
                                        </button>
                                    </div>
                                    <div class="col-xs-8 text-right">
                                        <button class="btn btn-xs btn-info" onclick="getList();">
                                            <i class="fa fa-refresh"></i> 刷新
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 查看 Modal -->
    <div class="modal fade" id="viewModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">Detail</h4>
                </div>

                <div class="modal-body">
                    <div class="row">
                        <form >
                            <div class="form-group col-xs-12">
                                <label class="col-xs-2 control-label text-right">
                                    <b class="text-right">Method</b>
                                </label>
                                <div class="col-xs-5">
                                    <span class="text-warning" id="method">GET</span>
                                </div>
                            </div>

                            <div class="form-group col-xs-12">
                                <label class="col-xs-2 control-label text-right">
                                    <b class="text-right">TaskID</b>
                                </label>
                                <div class="col-xs-5">
                                    <input type="text" value="" id="taskid" name="taskid" class="form-control input-sm">
                                </div>
                            </div>
                            
                            <div class="form-group col-xs-12">
                                <label class="col-xs-2 control-label text-right">
                                    <b class="text-right">URL</b>
                                </label>
                                <div class="col-xs-10">
                                    <input type="text" value="" id="url" name="url" class="form-control input-sm">
                                </div>
                            </div>
                            
                            <div class="form-group col-xs-12">
                                <label class="col-xs-2 control-label text-right">
                                    <b class="text-right">Payload</b>
                                </label>
                                <div class="col-xs-10">
                                    <input type="text" value="" id="payload" name="payload" class="form-control input-sm">
                                </div>
                            </div>

                            <div class="form-group col-xs-12">
                                <label class="col-xs-2 control-label text-right">
                                    <b class="text-right">PostData</b>
                                </label>
                                <div class="col-xs-10">
                                    <textarea class="form-control" id="postdata" rows="5"></textarea>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-xs" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <!-- jQuery -->
    <script src="{{ static_url('js/jquery.min.js') }}"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="{{ static_url('js/bootstrap.min.js') }}"></script>

    <!-- MetisMenu Plugin Js -->
    <script src="{{ 'http://cdn.bootcss.com/metisMenu/2.2.0/metisMenu.min.js' }}"></script>
    <!-- Base JavaScript -->
    <script src="{{ static_url('js/base.js') }}"></script>

    <script type="text/javascript">
        $(document).ready(function(){

            $('#side-menu').metisMenu();
            $('[data-toggle="tooltip"]').tooltip();

            // check All
            $('#all-checkbox').click(function(){
                if( $(this).prop('checked') )
                    $('td input[type="checkbox"]').prop('checked', true);
                else
                    $('td input[type="checkbox"]').prop('checked', false);
            });

            $('#btn-removeAll').on('click', function(){

            });

            var number = $('.table-bordered tbody tr').length;
            number == 0 && addItem( '<tr class="notList"><td colspan="8" class="text-center">Not Found List</td></tr>' );

        })

        // 显示模态框
        function viewModal(self)
        {
            self = self.parent().parent();
            taskid = self.find('td').eq(1).text().trim();
            url = self.find('td').eq(2).text().trim();
            method = self.find('td').eq(3).text().trim();
            postdata = self.find('td').eq(4).text().trim();
            payload = self.find('td').eq(5).text().trim();

            $('#url').val(url);
            $('#taskid').val(taskid);
            $('#method').text(method);
            $('#postdata').val(postdata);
            $('#payload').val(payload);

            $('#viewModal').modal('show');
        }


        // 添加到列表
        function addItem( params )
        {
            if( typeof params == 'object' )
            {
                var tr = [];
                tr.push('<tr>');
                tr.push( '<td class="text-center"><input type="checkbox"></td>' );
                tr.push( '<td><a href="' + params.url + '" data-toggle="tooltip" data-placement="bottom" title="' + params.url + '" target="_black">' + params.url + '</a></td>' );
                
                if( params.status == 1 )
                    tr.push( '<td class="text-center"><i class="status status-success" data-toggle="tooltip" data-placement="right" title="Start" onclick="reStart($(this), 0)"></i></td>' );
                else
                    tr.push( '<td class="text-center"><i class="status status-error" data-toggle="tooltip" data-placement="right" title="Error" onclick="reStart($(this), 1)"></i></td>' );

                tr.push( '<td class="wmax80"><a href="javascript:;" class="btn-xs btn-link btn-delete" onclick="removeItem($(this))">Delete</a></td>' );
                tr.push( '</tr>' );


                $('.table-trust tbody').append( tr.join('') );
            }
            else
            {
                $('.table-bordered tbody').append( params );
            }
        }



        // 删除列表
        function removeItem(self)
        {
            var taskid = self.parent().parent().children('td').children('input').val();
            
            $.post('/tasks/del', {taskid: taskid}, function(data){
                if (data == 'success') {
                    self.parent().parent().remove();
                    console.log('delete: '+taskid);
                }else{
                    console.log('delete error');
                }
            });

            
        }

        // 刷新列表
        function getList()
        {
            $.getJSON('/tasks', {v: 'json'}, function(data){
                var html = '';
                $.each(data, function(i, n){
                    html += '<tr>';
                    html += '<td class="text-center"><input type="checkbox" value="' +  n.taskid  + '"></td><td>' +  n.taskid  + '</td>';
                    html += '<td><a href="#" data-toggle="tooltip" data-placement="bottom" title="' +  n.url  + '"target="_black">' +  n.url  + '</a></td>';
                    html += '<td class="hidden-xs text-center text-info">' +  n.method  + '</td>'; 
                    html += '<td class="hidden-xs hidden-sm"><div class="text-space wmax100"data-toggle="tooltip" data-placement="bottom" title="' +  n.postData  + '">' +  n.postData  + '</div></td><td class="hidden-xs">' +  n.payload  + '</td>';
                    html += '<td class="hidden-xs">' +  n.parameter  + '</td>'; 
                    html += '<td class=""><a href="javascript:;" class="btn-xs btn-link" onclick="viewModal( $(this) );">View</a><a href="javascript:;" class="btn-xs btn-link" onclick="removeItem($(this))">Delete</a></td>'
                    html += '</tr>';
                });
                
                $("#tasklist").html(html);
            })

        }
    </script>
</body>
</html>
{% end %}